package com.yzd.learnjava.typeinfo.pets;

import java.util.LinkedHashMap;
import java.util.Map;

import com.yzd.learnjava.util.Print;

import javafx.util.Pair;


public class PetCount3 {
	static class PetCounter extends LinkedHashMap<Class<? extends Pet>, Integer>{
		public PetCounter() {
			for(Class<? extends Pet> c : LiteralPetCreator.allTypes) {
				put(c, 0);
			}
		}
		
		public void count(Pet pet) {
			for(Map.Entry<Class<? extends Pet>, Integer> pair : entrySet()){
				if(pair.getKey().isInstance(pet)) {
					put(pair.getKey(), pair.getValue() + 1);
				}
			}
		}
		
		public String toString() {
			StringBuilder sb = new StringBuilder("{");
			for(Map.Entry<Class<? extends Pet>, Integer> pair : entrySet()) {
				sb.append(pair.getKey().getSimpleName());
				sb.append("=");
				sb.append(pair.getValue());
				sb.append(", ");
			}
			sb.delete(sb.length() - 2, sb.length());
			sb.append("}");
			
			return sb.toString();
		}
	}
	
	public static void main(String[] args) {
		PetCounter counter = new PetCounter();
		for(Pet pet : Pets.createArray(20)) {
			Print.printnb(pet.getClass().getSimpleName() + " ");
			counter.count(pet);
		}
		Print.print();
		Print.print(counter);
	}
}
